#define UART0_BASE 0x10000000
#define UART0_THR (volatile unsigned char *)(UART0_BASE + 0x00)
#define UART0_LSR (volatile unsigned char *)(UART0_BASE + 0x05)

void uart_init(void) {
    // QEMU 的串口不需要特别初始化
}

void uart_putc(char c) {
    // 等待串口就绪
    while((*UART0_LSR & 0x20) == 0);
    *UART0_THR = c;
}

void uart_puts(const char *s) {
    while(*s) {
        uart_putc(*s++);
    }
}

char uart_getc(void) {
    // 等待有数据可读
    while((*UART0_LSR & 0x01) == 0);
    return *UART0_THR;
}

void uart_put_hex(unsigned long num) {
    int i;
    char c;
    uart_puts("0x");
    for (i = 60; i >= 0; i -= 4) {
        c = (num >> i) & 0xF;
        if (c < 10)
            uart_putc(c + '0');
        else
            uart_putc(c - 10 + 'A');
    }
}
